from FileHandlers.JsonFile import JReadSave
from data.Singleton import Singleton


class wildcards:

    def __init__(self, chara, enemy):
        self.chara: dict = chara
        self.enemy: dict = enemy
        self.mapper = {
            "$enemysextools$": self.enemy.get("name"),
            "$selfsextools": self.chara.get("name"),
            "$selfName$": self.chara.get("name"),
            "$enemyName$": self.enemy.get("name"),
            "$enemy$": self.chara.get("name"),
            "$self$": self.enemy.get("name"),
        }

    def replace(self, playscript: str):
        mapper = self.mapper
        for keyword, replace_target in mapper.items():
            playscript = playscript.replace(keyword, replace_target)

        return playscript


class skillLoader(metaclass=Singleton):
    def __init__(self, saveName=None):
        self.adapter_flag = False
        self.saveName = saveName
        self.json: dict = JReadSave(saveName=saveName, filename="skills")
    def reload(self):
        self.json: dict = JReadSave(saveName=self.saveName, filename="skills")

    def adapter(self):
        """
        技能信息的适配
        :return:
        """
        keyname = "技能名称"
        all_skill_json: dict = self.json.get(keyname)
        new = {}
        for index, skillName in all_skill_json.items():
            new[skillName] = {}
            new[skillName]['id'] = index

        copynew = new.copy()
        for attribute_name, attribute_mapper in self.json.items():
            if attribute_name == keyname:
                continue
            for skillname, value in new.items():
                value: dict
                id = value.get('id')
                copynew[skillname][attribute_name] = attribute_mapper.get(id)
        self.json = copynew

        self.adapter_flag = True

    def render_text(self, skills_name, self_chara: dict, enemy_chara: dict):
        """
        渲染技能的描述数据
        """
        playscript = self.json.get(skills_name).get("描述")
        playscript = wildcards(chara=self_chara, enemy=enemy_chara).replace(playscript=playscript)
        return playscript

    def release(self, skills_name, self_chara, enemy_chara):
        """
        释放技能
        :return:
        """
